var username = document.querySelector('[name="username"]')
var password = document.querySelector('[name="password"]')
var button = document.querySelector('[name="button"]')
button.onclick = function () {
    var reg = /^[a-z0-9]\w{4,11}$/
    var rag = /\w{6,12}/
    if (!reg.test(username.value) || !rag.test(password.value)) {
        alert("输入的账号或密码错误，请重新输入！")
        return false
    }
    var xhr = new XMLHttpRequest;
    xhr.open('post', 'http://localhost:8888/users/login')
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded')
    xhr.send(`username=${username.value}&password=${password.value}`)
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status >= 200 && xhr.status < 300) {
                var res = xhr.responseText
                console.log(res);
                res = JSON.parse(res)
                console.log(res);
                var { code, message,token,user } = res
                alert(message)
                if (code === 1) {
                    var {id}=user
                    location.href=`home.html?token=${token}&id=${id}`
                }
            }
        }
    }
}